package com.tuanbiao.ui.account;

import android.content.Context;
import android.content.Intent;

import com.jakewharton.rxbinding4.view.RxView;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.AccountController;
import com.tuanbiao.databinding.ActivityOfflineRegisterBinding;
import com.tuanbiao.dialog.WaitDialog;
import com.tuanbiao.models.AccountModel;
import com.tuanbiao.presenter.account.OfflineRegisterPresenter;

import java.util.concurrent.TimeUnit;

public class OfflineRegisterActivity extends BasePresenterActivity<OfflineRegisterPresenter, ActivityOfflineRegisterBinding> implements AccountController.OfflineRegisterView {
    private WaitDialog.Builder dialog;

    public static void start(Context context) {
        Intent starter = new Intent(context, OfflineRegisterActivity.class);
        context.startActivity(starter);
    }

    public static Intent createIntent(Context context) {
        return new Intent(context, OfflineRegisterActivity.class);
    }

    @Override
    protected ActivityOfflineRegisterBinding getBindingView() {
        return ActivityOfflineRegisterBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        RxView.clicks(binding.btnConfirm).throttleFirst(1, TimeUnit.MICROSECONDS)
                .subscribe(unit -> mPresenter.register(getString(binding.etIdCard), getString(binding.etName), getString(binding.etPassword)
                        , getString(binding.etPasswordConfirm), getString(binding.etCompanyName), getString(binding.etCompanyCode)
                        , getString(binding.etCreateCompany), getString(binding.etBlastCode)));
    }

    @Override
    protected OfflineRegisterPresenter createPresenter() {
        return new OfflineRegisterPresenter(new AccountModel(), this);
    }

    @Override
    public void showLoading() {
        if (null == dialog)
            dialog = new WaitDialog.Builder(getActivity()).setMessage("正在注册....");
        if (!dialog.isShowing()) dialog.show();
    }

    @Override
    public void hideLoading() {
        if (null != dialog && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void registerSuccess(String idCard, String password) {
        Intent intent = new Intent();
        intent.putExtra("_ID_CARD_", idCard);
        intent.putExtra("_PASSWORD_", password);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void showIdCard(String mIdCard) {
        binding.etIdCard.setText(mIdCard);
    }
}